var appPath = getAppPath();

$(function(){
	initJspGrid();
	initUploader();
});


// 模板组管理【YJX】
// 初始化加载 模板组  模板组相当于小分类 分类管理相当于大分类
function initJspGrid(){
	$('#tmplSetGrid').datagrid({
	    url:appPath + "/yjxTmpSet/getTmplSetInfoPg.action",
	    height:'full',
	    striped:true,
	    fitColumns:true,
	    nowrap: false,// 换行
	    queryParams: getQueryParam(),
	    singleSelect:false,
	    selectOnCheck:true,
	    checkOnSelect:true,
	    frozenColumns:[[
	        {field:'ck',checkbox:'true',width:10,align:'center',sortable:false}
	    ]],
	    columns:[[
            {field:'thumb',title:'缩略图',width:40,align:'center',sortable:false,
  	        	formatter:function(thumb, rowData, rowIndex){
  	        		var retUrl = '';
  					if (thumb) {
  						retUrl = '<a href="' + thumb + '" target="_blank">'
  							+ '<div style="display:table;text-align:center;height:50px;width:100%">'
  							+ '<span style="display:table-cell;vertical-align:middle">'
  							+ '<img src="' +thumb+'?x-oss-process=image/resize,m_lfit,h_120,w_100'
  							+ '" style="display:inline;max-height:50px;max-width:50px"></span></div></a>';
  					}
  					return retUrl;
  	        	}
            },
	        {field:'set_id',title:'模板组ID',width:30,align:'center',sortable:false},
		    {field:'edit',title:'编辑',width:30,align:'center',sortable:false,
					formatter:function(value,rowData,rowIndex){
						var ret = "<a href='javascript:void(0)'><img width='12px' height='12px' style='border:0px' src='"+appPath+"/js/lib/jquery-easyui/themes/icons/pencil.png' title='编辑' ";
						ret += " onClick='editTmplGp("+rowData.set_id+")'/></a>";
						return ret;
					}
			},
	        {field:'title',title:'编组名称',width:60,align:'left',sortable:false},
	        {field:'desc',title:'用途简介',width:70,align:'left',sortable:false,
	        	formatter:function(desc, rowData, rowIdx){
	        		var ret = rowData.desc;
	        		if(ret && ret.length>10) {
	        			return ret.substr(0,10)+'...';
	        		}
	        		return ret;
	        	}
	        },
	        {field:'order',title:'排序',width:20,align:'center',sortable:false,
		    	  formatter:function(value, rowData, rowIndex){
		    		  return "<a href='javascript:void(0)' onClick='editOrder("+rowData.set_id+","+rowData.order+")'>"+rowData.order+"</a>";
		    	  }
		    },
	        {field:'publish',title:'发布状态',width:35,align:'center',sortable:false,
	        	formatter:function(publish, rowData, rowIndex){
	        		if (Number(publish) == 1) return '<spna style="color:green;">已发布</span>';
	        		return '<span style="color:red;">未发布</span>';
	        	}
	        },
	        {field:'design', title:'内容', width:40, align:'center',sortable:false,
	        	formatter:function(value,rowData,rowIndex){
	        		return "<a href='javascript:void(0)' onclick='showTSRDesignLis(\""+rowData.title+"\","+rowData.set_id+","+rowData.use+")'>点击设置</a>";
	        	}
	        },
	        {field:'use',title:'用途',width:40,align:'center',sortable:false,
	        	formatter:function(value,rowData,rowIndex){
	        		var ret = '普通分类';
	        		switch (value){
	        		case 0: ret = '普通分类';break;
	        		case 1: ret = '<font color="#CD3700">行业分类</font>';break;
	        		case 10: ret = '<font color="#CD3700">专题分类</font>';break;
	        		case 20: ret = '<font color="#CD3700">转场视频</font>';break;
	        		case 21: ret = '<font color="#CD3700">片头视频</font>';break;
	        		case 22: ret = '<font color="#CD3700">片尾视频</font>';break;
	        		case 30: ret = '<font color="#CD3700">背景音乐</font>';break;
	        		}
	        		return ret;
	        	}
	        },
	        {field:'create_time',title:'创建时间',width:70,align:'center',sortable:false},
	        {field:'update_time',title:'更新时间',width:50,align:'center',sortable:false}
	    ]],
	    hideColumn:['user_type'],
	    pagination:true,
		rownumbers:true,
		toolbar:[{
				text:'添加模板组',
				iconCls:'icon-add',
				handler:function(){
					addTmplGp();
				}
			},
			'-',
			{
				text:'发布模板组',
				iconCls:'icon-ok',
				handler:function(){
					PubTmplGp();
				}
			},
			'-',
			{
				text:'取消发布',
				iconCls:'icon-no',
				handler:function(){
					cancelPubTmpGp();
				}
			},
			'-',
			{
				text:'删除模板组',
				iconCls:'icon-cancel',
				handler:function(){
					delTmplGp();
				}
			}]
	});
}

// 展示模板组下 已添加的设计模板列表
function showTSRDesignLis(title,id,type){
	$('#tDesignForm').form('clear');
	$("#set_id").val(id);
	$("#stType").val(type);// 分组分类
	
	$("#tsId").html(id);
	$("#stTitle").html(title);
	$('#tDesignDlg').window({
		title:'模板组:模板列表',
		iconCls:'icon-tip',
		modal: true,
		shadow: true,
		collapsible:false,
		minimizable:false,
		maximizable:false
	});
	$('#tDesignDlg').window('open');
	$('#tDesignDlg').window('move',{top:20,left:20});
	initTSRDesignGrid();
}

/* * Click to open the add template group page
 */
function addTmplGp(){
	$("#editTable").show();
	$('#editForm').form('clear');
	$("#setThumb").html('').val('');
	
	$("#editTmplUse").combobox('select',0).combobox('enable');
	$("#editBtn").show();
	$('#editDlg').window({
		title:'新建模板组',
		iconCls:'icon-add',
		modal: true,
		shadow: true,
		collapsible:false,
		minimizable:false,
		maximizable:false
	});
	
	$('#editDlg').window('move',{top:20,left:20});
	$('#editDlg').window('open');
}

/* Click to open the edit template group list
 */
function editTmplGp(setId){
	$("#editTable").show();
	$('#editForm').form('clear');
	$("#editSetId").val(setId);
	$("#setThumb").html('');
	
	var url = appPath + "/yjxTmpSet/getTmplSetLst.action?set_id="+ setId; // 获取单个模板组内容  可以修改
	$.get(url,function(ret){
		if (ret && ret.length>0){
			var info = ret[0];
			$("#editTmplTitle").val(info.title||'');
			$("#editTmplDesc").val(info.desc||'');
			$("#editTmplUse").combobox('select',info.use);
			var showImg = '';
			if(info.thumb && info.thumb.length>0) {
				showImg = info.thumb+'?x-oss-process=image/resize,m_pad,h_150,w_150,color_cccccc';
			}
			var imgHtml = '<a href="'+info.thumb+'" target="blank"><img src="'+showImg+'" style="display:inline;max-height:100px;max-width:200px"></a>';
			$("#setThumb").append(imgHtml).val(info.thumb||'');
		}
	});
	
	$("#editBtn").show();
	$('#editDlg').window({
		title:'编辑模板组',
		iconCls:'icon-edit',
		modal: true,
		shadow: true,
		collapsible:false,
		minimizable:false,
		maximizable:false
	});
	
	$('#editDlg').window('move',{top:20,left:20});
	$('#editDlg').window('open');
}

function cancelEdit(){
	$('#editDlg').window('close');
}

/* 	Submit the edit template group
 * 	确定新建或修改模板组信息
 */
function doEdit(){
	var setId = $("#editSetId").val();
	var title = $("#editTmplTitle").val();
	var desc = $("#editTmplDesc").val();
	var use = $("#editTmplUse").combobox('getValue');
	var imgUrl = $("#setThumb").val();
	
	if(title && title.length>0) {
		var url = appPath+"/yjxTmpSet/addTmplSet.action"
		var sendData = {
			"tmplSetDto.set_id": setId||"",
			"tmplSetDto.title": title || "",
			"tmplSetDto.desc": desc || "",
			"tmplSetDto.use":use||"",
			"tmplSetDto.thumb":imgUrl||""
		};
	    $.post(url, sendData, function(data){  
			if (data == "ok") { 
				$.messager.alert('提示信息','提交成功','info');
				$('#editDlg').window('close');
				doQuery();
	        } else {
	        	$.messager.alert('提示信息','提交失败','info');
	        }
	    });
	    
	}else{
		$.messager.alert('提示信息','请输入模板组名称','info');
	}
	
}

//删除模板组
function delTmplGp(){
	var selectItem=$("#tmplSetGrid").datagrid("getChecked");
	if(!selectItem || selectItem.length == 0){
		$.messager.alert('提示信息','请选择模板组！','warning');
		return;
	}
	for(var i=0; i<selectItem.length; i++){
		if(Number(selectItem[i].publish||'0')==1){
			$.messager.alert('提示信息','不能删除已发布模板组！','warning');
			return;
		}
	}
	
	$.messager.confirm("确认",
		"是否确认[删除]所选模板组？<br/><span style='color:red'>*对应的[模板组/模版关联信息]将一并删除!</span>",
		function(r){
	        if (r) { 
				var ids = "";
				$.each(selectItem, function(index, item){
					ids = ids + item.set_id + ",";
				});
				var url = appPath+"/yjxTmpSet/delTmplGp.action?ids="+ids;
				$.post(url,function(data){
					if(data=="ok"){
						$.messager.alert('提示信息','删除成功','info');
						doQuery();
					}else{
						$.messager.alert('提示信息','删除失败','info');
					}
				});
	        }
		}
	);
}

// 发布模板组
function PubTmplGp(){
	var selectTmplS=$("#tmplSetGrid").datagrid("getChecked");
	if(!selectTmplS || selectTmplS.length == 0){
		$.messager.alert('提示信息','请选择一个模板组进行处理！','warning');
		return;
	}else if(selectTmplS.length>1){
		$.messager.alert('提示信息','只能选择一个模板组！','warning');
		return;
	}
	
	var ids = "";
	var doFlg = false;
	$.each(selectTmplS, function(index, item){
		if(item.publish != 1 && item.publish != '1'){
			doFlg = true;
			ids = ids + item.set_id + ",";
		}
	});
	
	if(!doFlg){
		$.messager.alert('提示信息','请选择未发布的组进行处理！','warning');
		return;
	}
	
	$.messager.confirm("确认",
		"是否确认[发布]模板组？<br/><span style='color:red'>*请确保已经过认真检查!</span>",
		function (r) {
	        if (r) {
	        	var url = appPath+"/yjxTmpSet/publishTmplSet.action?ids="+ids;
	        	$.post(url,function(data){
	        		if(data=="ok"){
	        			$.messager.alert('提示信息','模板组发布成功！','info');
	        			doQuery();
	        		} else if (data=="ng"){
	        			$.messager.alert('提示信息','NG!','warning');
	        		}
	        	});
	        }
		}
	);
}

// 取消发布模板组
function cancelPubTmpGp(){
	var selectTmplS=$("#tmplSetGrid").datagrid("getChecked");
	if(!selectTmplS || selectTmplS.length == 0){
		$.messager.alert('提示信息','请选择已发布的模板组进行处理！','warning');
		return;
	}
	
	var ids = "";
	var doFlg = false;
	$.each(selectTmplS, function(index, item){
		if(item.publish == 1 || item.publish == '1'){
			doFlg = true;
			ids = ids + item.set_id + ",";
		}
	});
	
	if(!doFlg){
		$.messager.alert('提示信息','请选择已发布的模板组进行处理！','warning');
		return;
	}
	
	$.messager.confirm("确认", "是否确认[取消]已发布模板组？", function (r) {
		if(r){
			var url = appPath+"/yjxTmpSet/cancelPublishTmplSet.action?ids="+ids;
			$.post(url,function(data){
				if(data=="ok"){
					$.messager.alert('提示信息','模板组已取消发布！','info');
					doQuery();
				}
			});
		}
	});
}

// ** 修改顺序弹窗
function editOrder(tsId, order){
	$("#orderStId").val(tsId);
	$("#order").val(order);
	$("#orderBtn").show();
	$('#orderDlg').window({
		title:'显示顺序设定',
		iconCls:'icon-edit',
		modal: true,
		shadow: true,
		collapsible:false,
		minimizable:false,
		maximizable:false
	});
	
	$('#orderDlg').window('move',{top:20,left:20});
	$('#orderDlg').window('open');
}

// ** 确定修改顺序
function doUpdOrder(){
	var stId = $("#orderStId").val();
	var order = $("#order").val();
	if(!order || order.length == 0 || Number(order)>2000 || Number(order)<=0){
		$.messager.alert('提示信息','有效数值范围为：1～2000','warning');
		return;
	}
	var url = appPath+"/yjxTmpSet/upTmpSetOrder.action?setId="+stId+"&order="+order;
	$.post(url,function(data){
		if(data=="ok"){
			$.messager.alert('提示信息','显示顺序已更新！','info',function(){
				doQuery();
				$('#orderDlg').window('close');
			});
		}else {
			$.messager.alert('提示信息','显示顺序更新失败！','warning');
		}
	});
}

/** 结束显示顺序编辑*/
function cancelUpdOrder(){
	$('#orderDlg').window('close');
}

/**获取请求信息
 * */
function getQueryParam(){
	var queryObject = {};
	var findPubFlg = $("#queryPublish").combobox('getValue');//发布状态
	var findUse = $("#queryUse").combobox('getValue');// 用途
	var findTmplSTitle = $("#queryTmplTitle").val();
	var findTmplSDesc = $("#queryTmplDesc").val();
	var findTmplTsId = $("#queryTsId").val();
	
	if (Number(findUse)>=0) queryObject.findUse = findUse;
	if(findPubFlg && Number(findPubFlg||'-1')>=0) queryObject.findPubFlg = findPubFlg;// 发布标记
	if(findTmplSTitle && findTmplSTitle.length>0) queryObject.findTmplSTitle = findTmplSTitle;// 名称
	if(findTmplSDesc && findTmplSDesc.length>0) queryObject.findTmplSDesc = findTmplSDesc;// 用途介绍
	if(findTmplTsId && findTmplTsId.length>0) queryObject.findTmplTsId = findTmplTsId;// 模板组ID
	return queryObject;
}

// *  查询模板组
function doQuery(){
	$("#tmplSetGrid").datagrid('unselectAll');
	$("#tmplSetGrid").datagrid('load', getQueryParam());
}

// ** 查询模板组下的设计模板
function doQuery2(startFlg){
	$("#tDesignGrid").datagrid('unselectAll');
	if(startFlg==1){
		$("#tDesignGrid").datagrid('load', getQueryParam());
	} else {
		$("#tDesignGrid").datagrid('reload', getQueryParam());
	}
}

function getSetTopTypeNm(value){
	var tn = '';
	switch(Number(value||'0')){
  	case 1: tn='单页';break;
  	case 2: tn='多页';break;
  	case 3: tn='PPT';break;
  	case 4: tn='印品';break;
  	case 6: tn='社交媒体';break;
  	case 7: tn='电商';break;
  	case 8: tn='微商';break;
  	case 1010: tn='Logo商品';break;
  	case 1011: tn='营销商品';break;
  	case 1020: tn='视频模板';break;
  	case 1030: tn='视频背景';break;
  }
  return tn;
}

// 	** 加载模板组下的模板
function initTSRDesignGrid(){
	var setId=$("#set_id").val();
	$('#tDesignGrid').datagrid({
	    url:appPath + "/yjxTmpSet/getTmplSetRInfo.action?setId="+setId,
	    height:'full',
	    queryParams: getQueryParam5(),
	    striped:true,
	    fitColumns:true,
	    nowrap: false,// 换行
	    singleSelect:false,
	    selectOnCheck:true,
	    checkOnSelect:true,
	    frozenColumns:[[
	        {field:'ck',checkbox:'true',width:10,align:'center',sortable:false}
	    ]],
	    columns:[[
			{field:'theme_thumb',title:'缩略图',width:55,align:'center',sortable:false,
				formatter:function(theme_thumb, rowData, rowIndex){
					var retUrl = '';
					if (rowData.theme_thumb) {
						retUrl = '<a href="' + rowData.theme_thumb + '" target="_blank">'
							+ '<div style="display:table;text-align:center;height:110px;width:100%">'
							+ '<span style="display:table-cell;vertical-align:middle">'
							+ '<img src="' +rowData.theme_thumb+'?x-oss-process=image/resize,m_lfit,h_120,w_100'
							+ '" style="display:inline;max-height:100px;max-width:100px"></span></div></a>';
					}
					return retUrl;
				}
			},
	        {field:'uni_item_id',title:'统一模板ID',width:40,align:'center',sortable:false,
		    	formatter:function(id, rowData, rowIndex){
		    		return '<span style="color:red">'+id+'</span>'||'N/A';
		    	}
		    },
		    {field:'item_id',title:'模板ID',width:30,align:'center',sortable:false},
		    {field:'item_type',title:'分类',width:35,align:'center',sortable:false,
		    	formatter:function(value, rowData, rowIndex){// 0:未知, 10:单页, 20:PPT, 30:视频
		    		if(value){
		    			switch(value){
		    			case 10:ret = "<font color='green'>设计</font>";break;
		    			case 30:ret = "视频";break;
		    			case 50:ret = "<font color='#FF69B4'>音频</font>";break;
		    			}
		    			return ret;
		    		}
		    		return '未知';
		    	}
		    },
	        {field:'design_name',title:'名称',width:40,align:'center',sortable:false},
	        {field:'user_id',title:'用户ID',width:30,align:'center',sortable:false},
	        {field:'vip',title:'VIP',width:45,align:'center',sortable:false,
		    	  formatter:function(value, rowData, rowIndex){
		    		  if (rowData.item_type == 50){
			    			return '-';
			    	  }
		    		  var show = '普通模版';
		    		  if(Number(value||'0')==10) show="<span style='color:red;'>VIP模版</span>";
		    		  return show;
		    	  }
		    },
		    {field:'meal',title:'套餐',width:30,align:'center',sortable:false,
		    	formatter:function(value, rowData, rowIndex){
		    		if (rowData.item_type == 50){
		    			return '-';
		    		}
		    		if (rowData.price_id){
		    			return '<a href="javascript:void(0)" style="color:red;" onclick="showPrice('+rowData.uni_item_id+')">已设</a>';
		    		}
		    		return '<a href="javascript:void(0)" onclick="showPrice('+rowData.uni_item_id+')">设定</a>';
		    	}
		    },
		    {field:'order',title:'显示顺序',width:38,align:'center',sortable:false,
		    	  formatter:function(value, rowData, rowIndex){
		    		  if (value){
		    			  return "<a href='javascript:void(0)' onClick='editTmplOrder("+rowData.item_id+","+rowData.order+","+rowData.item_type+")'>"+rowData.order+"</a>";
		    		  }
		    		  return '-';
		    	  }
		    },
		    {field:'price_name',title:'价格套餐',width:45,align:'center',sortable:false,
		    	formatter:function(value, rowData, rowIndex){
		    		if (value){
		    			var unitPrice = rowData.unit_price/100||'0';
		    			return '<span style="color:#EE7942;">'+value+'</span><br/><font color="#696969">('+unitPrice+'/元)</font>';
		    		}
		    		return '<span style="color:red;">-</span>';
		    	}
		    },
		    {field:'publish',title:'发布状态',width:35,align:'center',sortable:false,
	        	formatter:function(publish, rowData, rowIndex){
	        		if (rowData.item_type == 50){
		    			return '-';
		    		}
	        		if (Number(publish) == 1) return '<span style="color:green">已发布</span>';
	        		return '<span style="color:red">未发布</span>';
	        	}
	        },
	        {field:'ver',title:'版本号',width:25,align:'center',sortable:false},
	        {field:'create_time',title:'创建时间',width:60,align:'center',sortable:false},
	        {field:'update_time',title:'更新时间',width:60,align:'center',sortable:false}
	    ]],
	    pagination:true,
		rownumbers:true,
		toolbar:[
		{
			text:'<span style="color:#2F4F4F;">添加[单页]模板</span>',
			iconCls:'icon-add',
			handler:function(){
				addDesignInfo();
			}
		},
		'-',
		{
			text:'<span style="color:#212121;">添加[视频]模板</span>',
			iconCls:'icon-add',
			handler:function(){
				addViDesignInfo(30);
			}
		},
		'-',
		{
			text:'<span style="color:#2F4F4F;">添加[音频]文件</span>',
			iconCls:'icon-add',
			handler:function(){
				addMedia();
			}
		},
		'-',
		{
			text:'移除模板',
			iconCls:'icon-no',
			handler:function(){
				delTSRDesign();
			}
		}]
	    
	});
}

// 修改模板组顺序
function editTmplOrder(designId, order,itemType){
	$("#tmplDesign").val(designId);
	$("#tmplOrder").val(order);
	$("#tmplOrderBtn").show();
	$("#itemType").val(itemType);
	
	$('#tmplOrderDlg').window({
		title:'显示顺序设定',
		iconCls:'icon-edit',
		modal: true,
		shadow: true,
		collapsible:false,
		minimizable:false,
		maximizable:false
	});
	$('#tmplOrderDlg').window('move',{top:20,left:20});
	$('#tmplOrderDlg').window('open');
}

function cancelEditOrder(){
	$('#tmplOrderDlg').window('close');
}

// 编辑顺序
function doEditOrder(){
	var designId = $("#tmplDesign").val();
	var order = $("#tmplOrder").val();
	if(!order || order.length == 0 || Number(order)>2000 || Number(order)<=0) {
		$.messager.alert('提示信息','有效数值范围为：1～2000','warning');
		return;
	}
	if(!designId || designId.length == 0 || Number(designId)<=0) {
		$.messager.alert('提示信息','模板ID参数有误','warning');
		return;
	}
	
	var type =$("#itemType").val();
	if (type == 30) {
		$.messager.confirm('提示信息','是否确认修改顺序?',function(r){
			if (r){
				editVideoOrder(designId,order)
			}
		});
	}
	
	if (type == 50 ){
		$.messager.confirm('提示信息','是否确认修改顺序?',function(r){
			if (r){
				editMediaOrder(designId,order)
			}
		});
	}
}

// 修改视频排序
function editVideoOrder(designId,order){
	var url = appPath+"/itbWebVideo/upVideoDesignInfo.action?ids="+designId+"&order="+order;
	$.post(url,function(data){
		doQuery5();
		if(data=="ok"){
			$('#tmplOrderDlg').window('close');
			$.messager.alert('提示信息','更新顺序成功 ','warning');
		}else {
			$.messager.alert('提示信息','更新顺序失败','warning');
		}
	});
}

// 修改音频排序
function editMediaOrder(designId,order){
	var url = appPath+"/yjxTmpSet/upMediaOrder.action?media_id="+designId+"&order="+order;
	$.post(url,function(data){
		doQuery5();
		if(data=="ok"){
			$('#tmplOrderDlg').window('close');
			$.messager.alert('提示信息','更新顺序成功 ','warning');
		}else {
			$.messager.alert('提示信息','更新顺序失败','warning');
		}
	});
}

// 添加适合的音频文件
function addMedia(){
	var tmpType = $("#stType").val();
	if (tmpType != 30) {
		return $.messager.alert('提示信息','仅在媒体分类下可添加','warning');
	}
	
	$('#mediaDlg').window({
		title:'音频文件列表',
		iconCls:'icon-edit',
		modal: true,
		shadow: true,
		collapsible:false,
		minimizable:false,
		maximizable:false
	});
	
	$('#mediaDlg').window('move',{top:20,left:20});
	$('#mediaDlg').window('open');
	initMedia();
}

// 初始化加载媒体列表
function initMedia(){
	var setId = $('#set_id').val();
	$('#mediaGrid').datagrid({
	    url:appPath + "/yjxTmpSet/getSuitMediaInfo.action?setId="+setId,
	    height:'450',
	    striped:true,
	    fitColumns:true,
	    nowrap: false,// 换行
	    queryParams: getMediaParam(),
	    singleSelect:false,
	    selectOnCheck:true,
	    checkOnSelect:true,
	    frozenColumns:[[
	        {field:'ck',checkbox:'true',width:10,align:'center',sortable:false}
	    ]],
	    columns:[[
  	        {field:'r_id',title:'添加状态',width:30,align:'center',sortable:false,
	        	formatter:function(value,rowData,rowIndex){
	        		if(value){
	        			return '<span style="color:red">已添加</span>';
	        		}
	        		return '<span style="color:green;">未添加</span>';
	        	}
	        },
	        {field:'uni_item_id',title:'统一ID',width:30,align:'center',sortable:false},
	        {field:'item_id',title:'媒体ID',width:35,align:'center',sortable:false},
		    {field:'user_id',title:'用户ID',width:50,align:'center',sortable:false},
	        {field:'media_name',title:'媒体名称',width:70,align:'left',sortable:false},
	        {field:'create_time',title:'创建时间',width:50,align:'center',sortable:false},
	        {field:'update_time',title:'更新时间',width:50,align:'center',sortable:false}
	    ]],
	    pagination:true,
		rownumbers:true
	});
}

function queryMedia(){
	$("#mediaGrid").datagrid('reload',getMediaParam());
}

// 
function getMediaParam(){
	var obj = {};
	var findName = $("#queryMediaName").val();
	var findMediaId = $("#queryMediaId").val();
	
	if (findName)obj.findMedaName = findName;
	if (findMediaId)obj.findTmplId = findMediaId;
	return obj;
}

// 展示价格套餐 列表
function showPrice(uni_item_id){
	$("#uitemId").val(uni_item_id);// 模板统一ID
	$('#mealTable').html('');
	if (uni_item_id==null || uni_item_id.length<=0){
		$.messager.alert('提示信息','该模板不能设定套餐信息！请联系管理员','warning');
		return;
	}
	
	var url = appPath+"/itbVideo/getVideoPriceMealInfo.action?uItemId="+uni_item_id;
	$.post(url,function(ret){	
		if(ret && ret.length>0){
			for(var i=0; i<ret.length; i++){
				var sub = ret[i];
				if(sub && sub.length>0){
					var itemHtml = '<tr><td class="queryTitle" width="100px">'+sub[0].type_name+'套餐</td>'+
							'<td class="queryContent" colspan="4">';
					for(var n=0; n<sub.length; n++){
						var chk = (Number(sub[n].uni_item_id||'0')>0)?"checked":"";
						itemHtml += '<div style="width:80px;float:left">'+
							'<input type="checkbox" '+chk+' id="price_'+sub[n].price_id+'" name="viPriceInfo" value="'+sub[n].price_id+'"/>'+
							sub[n].name+'</div>';
					}
					itemHtml += '</td></tr>';
					$('#mealTable').append(itemHtml);
				}
			}
		}
	});
	
	$("#mealBtn").show();
	$('#mealDlg').window({
		title:'模板套餐: 价格设定列表',
		iconCls:'icon-edit',
		modal: true,
		shadow: true,
		collapsible:false,
		minimizable:false,
		maximizable:false
	});
	
	$('#mealDlg').window('move',{top:20,left:20});
	$('#mealDlg').window('open');
}

// 模板组 下设置更新套餐情况
function updMeal(){
	var priceId = $("input[name='viPriceInfo']:checked").map(function(){
        	return $(this).val();
        }).get(); //  获取priceId
	var uitemId = $("#uitemId").val();// 模板统一ID
	$.messager.confirm("确认", "是否设置更新套餐？", function (r) {
		if(r){
			var url = appPath +"/itbWebVideo/addPriceRInfo.action";
			var data = {
					'uItemId':uitemId,
					'price_id':priceId.join(',')
			};
			$.post(url,data,function(ret){
				if(ret == 'ok'){
					$.messager.alert('提示信息','设置套餐成功','info');
					$("#tDesignGrid").datagrid('reload');
				} else {
					$.messager.alert('提示信息','设置套餐失败','warning');
				}
			});
		}
	});
}

function cancelMeal(){
	$('#mealDlg').window('close');
}

function validType(type){ // ** 判断分类的类型
	var ret = '其它';
		switch(type||'0'){
		case 0 : ret = '其它';break;
		case 10 : ret = '单页';break;
		case 20 : ret = 'PPT';break;
		case 30 : ret = '视频';break;
		}
	return ret;
}


//	添加视频模板 到模板组下面
function addViDesignInfo(tmplType){
	var setId = $('#set_id').val();
	var tmpType = $("#stType").val();
	if (tmpType == 30){
		return $.messager.alert('提示信息','音频组下仅能添加音频文件','warning');
	}
	
	var tmp = validType(tmplType);
	$.messager.confirm('确定','是否确定添加  <span style="color:red;">'+tmp+'</span> 下的设计模板？',function(r){
		if(r){
			$("#desBtn").show();
			$("#sViDesignForm").form('clear');
			$('#sViDesignDlg').window({
				title:'视频设计模板列表',
				iconCls:'icon-edit',
				modal: true,
				shadow: true,
				collapsible:false,
				minimizable:false,
				maximizable:false
			});
			$('#sViDesignDlg').window('move',{top:0,left:20});
			$('#sViDesignDlg').window('open');
			$("#queryVideoVip").combobox({
				data:[{'id':"-1",'name':'不限'},{'id':"0",'name':'普通模板'},{'id':"10",'name':'VIP模版'}],
				valueField:'id',
				textField:'name',
				value:-1
			});
			initSuitViDesign(tmplType);
		}
	});
}

// ** 移除设计模板组下的 设计模板
function delTSRDesign(){
	var selectItem=$("#tDesignGrid").datagrid("getChecked");
//	var type = $("#stType").val();
	var setId = $("#set_id").val();
	if(!selectItem || selectItem.length == 0){
		$.messager.alert('提示信息','请选择一个设计模板！！','warning');
		return;
	}
	$.messager.confirm("确认","确认移除该设计模板？",function(r){
	        if (r) { 
				var ids = "";
				$.each(selectItem, function(index, item){
					ids= ids + item.r_id+",";
				});
				var url = appPath+"/yjxTmpSet/delTmplSRDesign.action?rId="+ids;
				$.post(url,function(data){
					if(data=="ok"){
						$.messager.alert('提示信息','移除成功','info');
						doQuery2();
					}else{
						$.messager.alert('提示信息','移除失败','info');
					}
				});
	        }
		}
	);
}

/**模板组下设计模板de顺序*/
function editTSRorder(designId, order){
	$("#tsrDesignId").val(designId);
	$("#tsrOrder").val(order);
	$("#tsrOrderBtn").show();
	$('#tsrOrderDlg').window({
		title:'模板组下模板顺序',
		iconCls:'icon-edit',
		modal: true,
		shadow: true,
		collapsible:false,
		minimizable:false,
		maximizable:false
	});
	$('#tsrOrderDlg').window('move',{top:20,left:20});
	$('#tsrOrderDlg').window('open');
}

/**修改模板组下设计模板顺序
 * itb_tmpl_set_r
 * */
function upTsrOrder(){
	var designId = $("#tsrDesignId").val();
	var order = $("#tsrOrder").val();
	var tsId = $("#set_id").val();
	if(!order || order.length == 0 || Number(order)>999 || Number(order)<=0){
		$.messager.alert('提示信息','有效数值范围为：1～999','warning');
		return;
	}
	var url = appPath+"/tmpSet/updTsrOrder.action?designId="+designId+"&order="+order+"&tsId="+tsId;
	$.post(url,function(data){
		if(data=="ok"){
				$.messager.alert('提示信息','显示顺序已更新！','info',function(){
				doQuery2();
				$('#tsrOrderDlg').window('close');
			});
		}else {
			$.messager.alert('提示信息','显示顺序更新失败！','warning');
		}
	});
}

function cancelUpdTOrder(){
	$('#tsrOrderDlg').window('close');
}

//	适合的视频设计模板 列表 kris
function initSuitViDesign(tmplType){
	var setId = $('#set_id').val();
	$('#sViDesignGrid').datagrid({
	    url:appPath + "/yjxTmpSet/getViSuitDesignInfo.action?tmplType="+tmplType+"&setId="+setId,
	    height:'full',
	    striped:true,
	    fitColumns:true,
	    nowrap: false,// 换行
	    queryParams: getQueryParam3(),
	    singleSelect:false,
	    selectOnCheck:true,
	    checkOnSelect:true,
	    frozenColumns:[[
	        {field:'ck',checkbox:'true',width:10,align:'center',sortable:false}
	    ]],
	    columns:[[
  	        {field:'r_id',title:'添加状态',width:30,align:'center',sortable:false,
	        	formatter:function(value,rowData,rowIndex){
	        		if(value){
	        			return '<span style="color:red">已添加</span>';
	        		}
	        		return '<span style="color:green;">未添加</span>';
	        	}
	        },
	        {field:'theme_thumb',title:'缩略图',width:60,align:'center',sortable:false,
	        	formatter:function(thumbs, rowData, rowIndex){
	        		var retUrl = '';
					if (rowData.theme_thumb) {
						retUrl = '<a href="' + rowData.theme_thumb + '" target="_blank">'
							+ '<div style="display:table;text-align:center;height:90px;width:100%">'
							+ '<span style="display:table-cell;vertical-align:middle">'
							+ '<img src="' +rowData.theme_thumb+'?x-oss-process=image/resize,m_lfit,h_100,w_100'
							+ '" style="display:inline;max-height:90px;max-width:100px"></span></div></a>';
					}
					return retUrl;
	        	}
  	        },
	        {field:'uni_item_id',title:'统一ID',width:30,align:'center',sortable:false},
	        {field:'item_id',title:'模版ID',width:35,align:'center',sortable:false,
		    	formatter:function(id, rowData, rowIndex){
		    		var show = '<span style="color:blue;">'+id+'</span>';
		    		if(rowData.map_id){
		    			show = '<span style="color:blue;">'+id+'</span>'+
		    			'</br>'+
		    			'<span style="color:green;">'+rowData.map_id+'</span>';
		    		}
		    		return show;
		    	}
		    },
		    {field:'user_id',title:'设计师ID',width:50,align:'center',sortable:false,
		    	formatter:function(value, rowData, rowIndex){
		    		return rowData.nickname || rowData.user_name || rowData.user_id || '<span style="color:red">未知</span>';
		    	}
		    },
	        {field:'design_name',title:'模版名称',width:70,align:'left',sortable:false},
		    {field:'vip',title:'VIP',width:35,align:'center',sortable:false,
		    	  formatter:function(value, rowData, rowIndex){
		    		  var show = "<span style='color:green;'>普通模板</span>";
		    		  if(Number(value||'0')==10) show="<span style='color:red;'>VIP模版</span>";
		    		  return show;
		    	  }
		    },
	        {field:'publish',title:'发布状态',width:40,align:'center',sortable:false,
	        	formatter:function(publish, rowData, rowIndex){
	        		if (Number(publish) == 1) return '<span style="color:green">已发布</span>';
	        		return '<span style="color:red">未发布</span>';
	        	}
		    },
	        {field:'create_time',title:'创建时间',width:50,align:'center',sortable:false},
	        {field:'update_time',title:'更新时间',width:50,align:'center',sortable:false}
	    ]],
	    pagination:true,
		rownumbers:true
	});
}

//	添加设计模板  到分组下  添加到分组
function addToTmplSr(s){
	var checkRows = '';
	if(s === 1){
		checkRows = $("#sViDesignGrid").datagrid('getChecked'); // 加载视频设计模板
	} else if (s == 2){
		checkRows = $("#mediaGrid").datagrid('getChecked'); // 媒体文件
	}else{
		checkRows = $("#sDesignGrid").datagrid('getChecked');// 加载单页
	}
	
	var setId = $("#set_id").val();
	if (checkRows.length==0) {
		$.messager.alert('提示信息','请选择需要添加的作品！','info');
		return;
	} else {
		var ids ='';
		var info = [];
		for (var i=0;i<checkRows.length;i++) {
			var st = Number(checkRows[i].r_id);//根据是否ts_id>0  判断是否已经添加过模板
			if (st<=0) {
				 ids = ids + checkRows[i].uni_item_id+',';
			} else {
				info.push(checkRows[i].uni_item_id);
			}
		}
		if (info.length>0) {
			$.messager.alert('提示信息','不能勾选已添加过的作品!','info');
			return;
		}
		$.messager.confirm("确认","确定将该作品 添加到该分组下？？",function(r){
	        if (r) { 
				var url = appPath+"/yjxTmpSet/addSuitDesign.action";
	        	var data = {"setId":setId,"uTmplId":ids,"type":s};
				$.post(url,data,function(data){
					if(data=="ok"){
						$.messager.alert('提示信息','添加成功！','info');
						doQuery2();
						if (s == 1){
							doQuery3();
						} else if (s == 2) {
							$("#mediaGrid").datagrid('reload', getMediaParam());
						} else{
							doQuery4();
						}
					}else if(data=="exit"){
						$.messager.alert('提示信息','该模板组下已经存在该作品','info');
					}else if(data=="no"){
						$.messager.alert('提示信息','添加失败','info');
						doQuery4();
					}
				});
	        }
		});
	}
}

// ** 参数 适合的设计模板
function getQueryParam3(){
	var queryObject = {};
	var findVip = $("#queryVideoVip").combobox('getValue');
	var findTmplName=$("#queryTmplName").val();
	var findTmplId=$("#queryTmplId").val();
	if(findVip&&findVip.length>0&&Number(findVip)>=0) queryObject.findVip = findVip;// vip
	if(findTmplName&&findTmplName.length>0) queryObject.findTmplName = findTmplName;// 模版名称
	if(findTmplId&&findTmplId.length>0) queryObject.findTmplId = findTmplId;// 模版id
	return queryObject;
}

//  ** 查询适合的设计模板
function doQuery3(startFlg){
	$("#sViDesignGrid").datagrid('unselectAll');
	if(startFlg == 1) {
		$("#sViDesignGrid").datagrid('load', getQueryParam3());
	} else {
		$("#sViDesignGrid").datagrid('reload', getQueryParam3());
	}
}

// ** 参数 适合的设计模板
function getQueryParam5(){
	var queryObject = {};
	var findTgId=$("#queryTgId").val();
	var findTgTitle=$("#queryTgTitle").val();
	if(findTgId&&findTgId.length>0&&Number(findTgId)>=0) queryObject.findTgId = findTgId;
	if(findTgTitle&&findTgTitle.length>0) queryObject.findTgTitle = findTgTitle;// 模版名称
	return queryObject;
}

//  ** 查询适合的设计模板
function doQuery5(startFlg){
	$("#tDesignGrid").datagrid('unselectAll');
	if(startFlg == 1) {
		$("#tDesignGrid").datagrid('load', getQueryParam5());
	} else {
		$("#tDesignGrid").datagrid('reload', getQueryParam5());
	}
}


//  添加适合的设计模板
function addDesignInfo(){
	var setId = $('#set_id').val();
	var tmpType = $("#stType").val();
	if (tmpType == 30){
		return $.messager.alert('提示信息','请点击添加音频文件','warning');
	}
	
	
	$("#desBtn").show();
	$("#sDesignForm").form('clear');
	$('#sDesignDlg').window({
		title:'[单页]模板列表',
		iconCls:'icon-edit',
		modal: true,
		shadow: true,
		collapsible:false,
		minimizable:false,
		maximizable:false
	});
	
	// 大分类
	$("#queryTmplType").combobox({
		url:appPath+'/itbTg/getTypeForCombo.action?topIds=1,2,3,4,6,7,8,1010,1011,1030&blkFlg=1',
		valueField:'type_id',
		textField:'type_name',
		value : 1030,
		onSelect:function(selRecd){
			$("#queryTmplType2").combobox("clear");
			$("#queryTmplType3").combobox("clear");
			$("#queryTmplType2").combobox("loadData",{});
			$("#queryTmplType3").combobox("loadData",{});
			// 中分类
			var parentId=selRecd.type_id;
			if(Number(parentId)>0){
				$("#queryTmplType2").combobox({
					url:appPath+'/itbTg/getTypeForCombo.action?parent='+parentId+'&blkFlg=1',
					valueField:'type_id',
					textField:'type_name',
					onSelect:function(selRecd2){
						$("#queryTmplType3").combobox("clear");
						$("#queryTmplType3").combobox("loadData",{});
						var parentId2=selRecd2.type_id;
						if(Number(parentId2)>0){
							$("#queryTmplType3").combobox({
								url:appPath+'/itbTg/getTypeForCombo.action?parent='+parentId2+'&blkFlg=1',
								valueField:'type_id',
								textField:'type_name'
							});
						}
					}
				});
			}
		},
		onLoadSuccess:function(){
			$('#queryTmplType').combobox('select', 1030);
		}
	});
	
	$('#sDesignDlg').window('move',{top:0,left:20});
	$('#sDesignDlg').window('open');
	$("#queryDesignVip").combobox({
		data:[{'id':"-1",'name':'不限'},{'id':"0",'name':'普通模板'},{'id':"10",'name':'VIP模版'}],
		valueField:'id',
		textField:'name',
		value:-1
	});
	initSuitDesign(); //  加载适合的设计模板
}

//	适合的设计模板信息
function initSuitDesign(){
	var setId = $('#set_id').val();
	$('#sDesignGrid').datagrid({
	    url:appPath + "/yjxTmpSet/getSuitDesignInfo.action?setId="+setId,
	    height:'full',
	    striped:true,
	    fitColumns:true,
	    nowrap: false,// 换行
	    queryParams: getQueryParam4(),
	    singleSelect:false,
	    selectOnCheck:true,
	    checkOnSelect:true,
	    frozenColumns:[[
	        {field:'ck',checkbox:'true',width:10,align:'center',sortable:false}
	    ]],
	    columns:[[
  	        {field:'r_id',title:'添加状态',width:35,align:'center',sortable:false,
	        	formatter:function(value,rowData,rowIndex){
	        		if(value){
	        			return '<span style="color:red">已添加</span>';
	        		}
	        		return '<span style="color:green;">未添加</span>';
	        	}
	        },
  	        {field:'thumb',title:'缩略图',width:65,align:'center',sortable:false,
	        	formatter:function(thumb, rowData, rowIndex){
	        		var retUrl = '';
					if (rowData.thumb) {
						retUrl = '<a href="' + rowData.thumb + '" target="_blank">'
							+ '<div style="display:table;text-align:center;height:120px;width:100%">'
							+ '<span style="display:table-cell;vertical-align:middle">'
							+ '<img src="' +rowData.thumb+'?x-oss-process=image/resize,m_lfit,h_120,w_100'
							+ '" style="display:inline;max-height:110px;max-width:100px"></span></div></a>';
					}
					return retUrl;
	        	}
  	        },
	        {field:'design_id',title:'模版ID',width:30,align:'center',sortable:false,
		    	formatter:function(id, rowData, rowIndex){
		    		var show = '<span style="color:blue;">'+id+'</span>';
		    		if(rowData.map_id){
		    			show = '<span style="color:blue;">'+id+'</span>'+
		    			'</br>'+
		    			'<span style="color:green;">'+rowData.map_id+'</span>';
		    		}
		    		return show;
		    	}
		    },
	        {field:'size',title:'尺寸(W*H)',width:50,align:'center',sortable:false,
		    	formatter:function(value, rowData, rowIndex){
		    		return ''+(rowData.width||'N/A')+
		    			'<span style="color:green">*</span>'+
		    			(rowData.height||'N/A')+'';
		    	}
		    },
		    {field:'top_type',title:'主分类',width:35,align:'center',sortable:false,
		    	formatter:function(value, rowData, rowIndex){
		    		var tn = getTopTypeNm(value);
		    		if(!tn || tn.length==0) {
		    			tn = '<span style="color:red">未知</span>';
		    		}
		    		return tn;
		    	}
		    },
	        {field:'editType',title:'子分类',width:35,align:'center',sortable:false,
		    	formatter:function(value, rowData, rowIndex){
		    		return rowData.type_name || '<span style="color:red">未分类</span>';
		    	}
		    },
		    {field:'user_id',title:'设计师',width:45,align:'center',sortable:false,
		    	formatter:function(value, rowData, rowIndex){
		    		return rowData.nickname || rowData.user_name || rowData.user_id || '<span style="color:red">未知</span>';
		    	}
		    },
		    {field:'item_id',title:'关联商品',width:35,align:'center',sortable:false,
		    	formatter:function(value, rowData, rowIndex){
		    		var topType = Number(rowData.top_type||'0');
		    		if(topType!=4 && topType!=1010 && topType!=1011) {
		    			return '<span style="color:#FF3030;">N/A</span>';
		    		}
		    		if(Number(value||'0')==0) {
		    			return '<span style="color:blue">未关联</span>';
		    		}
		    		return value;
		    	}
		    },
	        {field:'design_name',title:'模版名称',width:70,align:'center',sortable:false},
		    {field:'vip',title:'VIP',width:40,align:'center',sortable:false,
		    	  formatter:function(value, rowData, rowIndex){
		    		  var show = "<span style='color:green;'>普通模板</span>";
		    		  if(Number(value||'0')==10) show="<span style='color:red;'>VIP模版</span>";
		    		  return show;
		    	  }
		    },
	        {field:'publish',title:'发布状态',width:35,align:'center',sortable:false,
	        	formatter:function(publish, rowData, rowIndex){
	        		if (Number(publish) == 1) return '<span style="color:green">已发布</span>';
	        		return '<span style="color:red">未发布</span>';
	        	}
		    },
	        {field:'create_time',title:'创建时间',width:50,align:'center',sortable:false},
	        {field:'update_time',title:'更新时间',width:50,align:'center',sortable:false}
	    ]],
	    pagination:true,
		rownumbers:true
	});
}

function getTopTypeNm(value){
	var tn = '';
	switch(Number(value||'0')){
  	case 1: tn='单页';break;
  	case 2: tn='多页';break;
  	case 3: tn='PPT';break;
  	case 4: tn='<span style="color:red">印品</span>';break;
  	case 6: tn='社交媒体';break;
  	case 7: tn='电商';break;
  	case 8: tn='微商';break;
  	case 1010: tn='Logo商品';break;
  	case 1011: tn='营销商品';break;
  	case 1030: tn='视频背景';break;
  }
  return tn;
}

/*
 *  获取 请求信息
 */
function getQueryParam4(){
	var queryObject = {};
	var findTmplType =$("#queryTmplType3").combobox('getValue');
	if(!findTmplType || findTmplType == 0){
		findTmplType =$("#queryTmplType2").combobox('getValue');
		if(!findTmplType || findTmplType == 0){
			findTmplType=$("#queryTmplType").combobox('getValue');
		}
	}
	if (Number(findTmplType)>0) {
		queryObject.findTmplType = findTmplType;// 类型
	}
	var findVip = $("#queryDesignVip").combobox('getValue');
	var findTmplName=$("#queryDesTmplName").val();
	var findTmplId=$("#queryDesTmplId").val();
	if(findVip&&findVip.length>0&&Number(findVip)>=0) queryObject.findVip = findVip;// vip
	if(findTmplName&&findTmplName.length>0) queryObject.findTmplName = findTmplName;// 模版名称
	if(findTmplId&&findTmplId.length>0) queryObject.findTmplId = findTmplId;// 模版id
	return queryObject;
}

/**查询数据*/
function doQuery4(startFlg){
	$("#sDesignGrid").datagrid('unselectAll');
	if(startFlg == 1) {
		$("#sDesignGrid").datagrid('load', getQueryParam4());
	} else {
		$("#sDesignGrid").datagrid('reload', getQueryParam4());
	}
}

// 上传图片
function initUploader(){
	var url = "http://upload.itbour.com/upload/user?device=101&user_type=0&user_id="+(g_userId||0);
    var option = {};
    option.id = 'selImgFile';
    option.url = url;
    option.callback = function(res){
    	$("#setThumb").html('');
    	if(!res.success){
            return;
    	}
    	if(res && res.data && res.data.url){
    		var showImg = '';
			if(res.data.url && res.data.url.length>0) {
				showImg = res.data.url+'?x-oss-process=image/resize,m_pad,h_150,w_150,color_cccccc';
			}
			var imgHtml = '<a href="'+res.data.url+'" target="blank"><img src="'+showImg+'" style="display:inline;max-height:100px;max-width:200px"></a>';
			$("#setThumb").append(imgHtml).val(res.data.url||'');
    	}
    }
    option.imgExts = ['jpg','jpeg','png'];
    //option.bindInputFileName ='fileNameInput';
    var uploader = new AjaxUploadEx(option);
}

function delSelImage(){
	$("#setThumb").html('');
	$("#setThumb").val('');
}